package com.qf04;

public class TestBankCard {

    public static void main(String[] args) {
        BankCard card = new BankCard();

        Runnable add = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    synchronized (card) {
                        card.setMoney(card.getMoney() + 1000);
                        System.out.println(Thread.currentThread().getName() + "存了1000，余额是:" + card.getMoney());
                    }
                }
            }
        };

        Runnable sub = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    synchronized (card) {
                        if (card.getMoney() >= 1000) {
                            card.setMoney(card.getMoney() - 1000);
                            System.out.println(Thread.currentThread().getName() + "取了1000，余额是:" + card.getMoney());
                        } else {
                            System.out.println("余额不足，请存取");
                            i--;
                        }
                    }
                }
            }
        };
        Thread xiaoli = new Thread(add, "小李");
        Thread xiaoyue = new Thread(sub, "小月");
        xiaoli.start();
        xiaoyue.start();
    }
}
